package com.cz.zslu.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.cz.zslu.common.ReturnObject;
import com.cz.zslu.entity.ZslStoreCommodity;
import com.cz.zslu.entity.ZslStoreCommodityType;
import com.cz.zslu.interceptor.BaseContextHandler;
import com.cz.zslu.interceptor.IgnoreAuth;
import com.cz.zslu.model.Ztree;
import com.cz.zslu.rest.BaseController;
import com.cz.zslu.service.ISysUserService;
import com.cz.zslu.service.IZslStoreCommodityService;
import com.cz.zslu.service.IZslStoreCommodityTypeService;
import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 商品分类 前端控制器
 * </p>
 *
 * @author 森森
 * @since 2018-10-25
 */
@RestController
@RequestMapping("/sys/storeCommodityType")
@Api(description = "商品分类")
public class StoreCommodityTypeController extends BaseController<IZslStoreCommodityTypeService, ZslStoreCommodityType> {

    @Autowired
    private IZslStoreCommodityService iZslStoreCommodityService;
    @Autowired
    private ISysUserService iSysUserService;

    @ApiOperation("获取全部分类分页")
    @IgnoreAuth
    @RequestMapping(value = "/getAllPage", method = RequestMethod.POST)
    public ReturnObject<List<ZslStoreCommodityType>> getAllPage(@ApiParam("每页条数") @RequestParam(defaultValue = "10") int limit,
                                                                @ApiParam("页数") @RequestParam(defaultValue = "1") int offset) {

        EntityWrapper<ZslStoreCommodityType> ew = new EntityWrapper<>();
        ew.eq("state", 0);
        ew.orderBy("crt_time", false);
        Page<ZslStoreCommodityType> obj = baseService.selectPage(new Page<>(offset, limit), ew);

        return new ReturnObject<>(0, "获取成功", obj.getTotal(), obj.getPages(), obj.getRecords());
    }


    @ApiOperation("获取店铺的分配")
    @IgnoreAuth
    @RequestMapping(value = "/getStoreType", method = RequestMethod.POST)
    public ReturnObject<List<ZslStoreCommodityType>> getStoreType(@ApiParam("店铺id") @RequestParam Long storeId) {
        List<ZslStoreCommodityType> storeType = Lists.newArrayList();
        List<ZslStoreCommodityType> list = baseService.selectList(new EntityWrapper<ZslStoreCommodityType>().isNull(
                "pid"));
        for (ZslStoreCommodityType info : list) {
            if (iZslStoreCommodityService.selectCount(new EntityWrapper<ZslStoreCommodity>().eq("state", 0).eq(
                    "store_id", storeId).like("commodity_type", info.getName())) > 0) {
                storeType.add(info);
                //获取二级
                List<ZslStoreCommodityType> list2 =
                        baseService.selectList(new EntityWrapper<ZslStoreCommodityType>().eq("pid", info.getId()));
                for (ZslStoreCommodityType type : list2) {
                    if (iZslStoreCommodityService.selectCount(new EntityWrapper<ZslStoreCommodity>().eq("state", 0).eq(
                            "store_id", storeId).like("commodity_type", type.getName())) > 0) {
                        info.getSubordinate().add(type);
                    }

                }

            }
        }

        return new ReturnObject<>(0, "获取成功", storeType);
    }

    @ApiOperation("获取全部分类")
    @IgnoreAuth
    @RequestMapping(value = "/getAllList", method = RequestMethod.POST)
    public ReturnObject<List<ZslStoreCommodityType>> getAllList() {

        EntityWrapper<ZslStoreCommodityType> ew = new EntityWrapper<>();
        ew.orderBy("crt_time", true);
        ew.isNull("pid");
        List<ZslStoreCommodityType> obj = baseService.selectList(ew);
        for (int i = 0; i < obj.size(); i++) {
            obj.get(i).setSubordinate(baseService.selectList(new EntityWrapper<ZslStoreCommodityType>().eq("pid",
                    obj.get(i).getId())));
            for (int j = 0; j < obj.get(i).getSubordinate().size(); j++) {
                obj.get(i).getSubordinate().get(j).setSubordinate(baseService.selectList(new EntityWrapper<ZslStoreCommodityType>().eq("pid",
                        obj.get(i).getSubordinate().get(j).getId())));
            }
        }

        return new ReturnObject<>(0, "获取成功", obj);
    }

    @ApiOperation("获取常用分类")
    @RequestMapping(value = "/getCommonList", method = RequestMethod.POST)
    public ReturnObject<List<ZslStoreCommodityType>> getCommonList() {
        Long storeId = iSysUserService.selectById(BaseContextHandler.getUserID()).getStoreId();
        List<ZslStoreCommodityType> storeType = Lists.newArrayList();
        List<ZslStoreCommodityType> list = baseService.selectList(new EntityWrapper<ZslStoreCommodityType>().isNull(
                "pid"));
        for (ZslStoreCommodityType info : list) {
            if (iZslStoreCommodityService.selectCount(new EntityWrapper<ZslStoreCommodity>().eq("state", 0).eq(
                    "store_id", storeId).like("commodity_type", info.getName())) > 0) {
                storeType.add(info);
                //获取二级
                List<ZslStoreCommodityType> list2 =
                        baseService.selectList(new EntityWrapper<ZslStoreCommodityType>().eq("pid", info.getId()));
                for (ZslStoreCommodityType type : list2) {
                    if (iZslStoreCommodityService.selectCount(new EntityWrapper<ZslStoreCommodity>().eq("state", 0).eq(
                            "store_id", storeId).like("commodity_type", type.getName())) > 0) {
                        info.getSubordinate().add(type);
                        List<ZslStoreCommodityType> list3 =
                                baseService.selectList(new EntityWrapper<ZslStoreCommodityType>().eq("pid",
                                        type.getId()));
                        for (ZslStoreCommodityType typeer : list3) {
                            if (iZslStoreCommodityService.selectCount(new EntityWrapper<ZslStoreCommodity>().eq(
                                    "state", 0).eq(
                                    "store_id", storeId).like("commodity_type", typeer.getName())) > 0) {
                                type.getSubordinate().add(typeer);
                            }
                        }

                    }

                }

            }
        }

        return new ReturnObject<>(0, "获取成功", storeType);
    }

    @ApiOperation("获取全部树结构")
    @IgnoreAuth
    @RequestMapping(value = "/getAllListTree", method = RequestMethod.POST)
    public ReturnObject<List<Ztree>> getAllListTree() {

        EntityWrapper<ZslStoreCommodityType> ew = new EntityWrapper<>();
        ew.orderBy("crt_time", true);
        List<ZslStoreCommodityType> obj = baseService.selectList(ew);
        List<Ztree> ztreeList = Lists.newArrayList();
        for (ZslStoreCommodityType info : obj) {
            if (info.getPid() == null) {
                ztreeList.add(new Ztree(info.getId().toString(), null, info.getName()));
            } else {
                ztreeList.add(new Ztree(info.getId().toString(), info.getPid().toString(), info.getName()));
            }

        }
        return new ReturnObject<>(0, "获取成功", ztreeList);
    }

    @IgnoreAuth
    @ApiOperation("获取一级分类")
    @RequestMapping(value = "/getListByPidIsNull", method = RequestMethod.POST)
    public ReturnObject<List<ZslStoreCommodityType>> getListByPidIsNull() {
        EntityWrapper<ZslStoreCommodityType> ew = new EntityWrapper<>();
        ew.where("pid is null", "null").eq("state", 0);
        ew.orderBy("crt_time", true);
        return new ReturnObject<>(0, "获取成功", baseService.selectList(ew));
    }

    @IgnoreAuth
    @ApiOperation("获取二级级分类")
    @RequestMapping(value = "/getListByPid", method = RequestMethod.POST)
    public ReturnObject<List<ZslStoreCommodityType>> getListByPid(@ApiParam("id") @RequestParam Long id) {
        EntityWrapper<ZslStoreCommodityType> ew = new EntityWrapper<>();
        ew.eq("pid", id).eq("state", 0);
        ew.orderBy("crt_time", true);
        return new ReturnObject<>(0, "获取成功", baseService.selectList(ew));
    }

    @ApiOperation("获取详情")
    @RequestMapping(value = "/findInfoById", method = RequestMethod.POST)
    public ReturnObject<ZslStoreCommodityType> findInfoById(@ApiParam("id") @RequestParam Long id) {

        return new ReturnObject<>(0, "新增成功", baseService.selectById(id));
    }

    @ApiOperation("新增")
    @RequestMapping(value = "/saveStoreCommodityTypeInfo", method = RequestMethod.POST)
    public ReturnObject<ZslStoreCommodityType> saveStoreCommodityTypeInfo(@ApiParam("名称") @RequestParam String name,
                                                                          @ApiParam("上级id") Long pid) {

        ZslStoreCommodityType info = new ZslStoreCommodityType();
        info.setName(name);
        info.setPid(pid);
        baseService.insert(info);

        return new ReturnObject<>(0, "新增成功", null);
    }

    @ApiOperation("删除")
    @RequestMapping(value = "/delById", method = RequestMethod.POST)
    public ReturnObject<ZslStoreCommodityType> delById(@ApiParam("id") @RequestParam Long id) {

        ZslStoreCommodityType info = baseService.selectById(id);
        info.setState(1);

        baseService.updateById(info);

        return new ReturnObject<>(0, "删除成功", null);
    }


    @IgnoreAuth
    @ApiOperation("获取相关零部件")
    @RequestMapping(value = "/getPartsListByStair", method = RequestMethod.POST)
    public ReturnObject<List<ZslStoreCommodityType>> getPartsListByStair() {
        //首先获取一级分类
        List<ZslStoreCommodityType> stairs=baseService.selectList(new EntityWrapper<ZslStoreCommodityType>().where("pid is null", "null").eq("state", 0).orderBy("crt_time", true));
        List<Long> ids=new ArrayList<Long>();
        if(stairs!=null&&stairs.size()>0){
            for(ZslStoreCommodityType zslStoreCommodityType:stairs){
                ids.add(zslStoreCommodityType.getId());
            }
        }
        //获取二级分类
        List<ZslStoreCommodityType> sencondLevel=baseService.selectList(new EntityWrapper<ZslStoreCommodityType>().eq("state", 0).in("id",ids).orderBy("crt_time", true));

        return new ReturnObject<>(0, "获取成功", sencondLevel);
    }

    @IgnoreAuth
    @ApiOperation("获取相关热门")
    @RequestMapping(value = "/getHotListBySecond", method = RequestMethod.POST)
    public ReturnObject<List<ZslStoreCommodityType>> getHotListBySecond(@ApiParam("二级分类id") @RequestParam Long id) {
        //通过二级分类获取三级分类
        List<ZslStoreCommodityType> thirdLevel=baseService.selectList(new EntityWrapper<ZslStoreCommodityType>().eq("state", 0).eq("pid",id).orderBy("crt_time", true));
        return new ReturnObject<>(0, "获取成功", thirdLevel);
    }

}

